<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-row :gutter="15">
        <el-col :span="12">
          <el-form-item label="商品编码" prop="code">
            <el-input v-model="dataForm.code" placeholder="商品编码" :disabled="isEdit"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商品名称" prop="goodsName">
            <el-input v-model="dataForm.goodsName" placeholder="商品名称" :disabled="isEdit"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :span="8">
          <el-form-item label="制造商" prop="supplyId">
            <el-select v-model="dataForm.supplyId" placeholder="请选择" :disabled="isEdit" @change="selectSupplyChange($event)">
              <el-option v-for="item in supplyOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="品种" prop="brandId">
            <el-select v-model="dataForm.brandId" placeholder="请选择" :disabled="isEdit" @change="selectBrandChange($event)">
              <el-option v-for="item in brandOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="仓位名称" prop="stockId">
            <el-select v-model="dataForm.stockId" placeholder="请选择" :disabled="isEdit" @change="selectStockChange($event)">
              <el-option v-for="item in stockOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :span="8">
          <el-form-item label="库存上限" prop="inventoryTop">
            <el-input v-model="dataForm.inventoryTop" placeholder="库存上限"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="库存下限" prop="inventoryFloor">
            <el-input v-model="dataForm.inventoryFloor" placeholder="库存下限"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="红色预警" prop="redAlert">
            <el-input v-model="dataForm.redAlert" placeholder="红色预警"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getWarningSettingById,warningSettingEdit,getSupplyDataList,getGoodsBrandDataList,getStockDataList} from '@/api/eims/warning-setting'
    export default {
    data () {
      return {
        ischeck: false,
        visible: false,
        isEdit: false,
        dataForm: {
          id: 0,
          name: ''
        },
        supplyOptions: [],
        brandOptions: [],
        stockOptions: [],
        dataRule: {
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.getSupplyList()
        this.getBrandDataList()
        this.getStockList()
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.isEdit = true
            getWarningSettingById(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
                this.selectSupplyChange(this.dataForm.supplyId) // 有supplyName,不用写
                this.selectBrandChange(this.dataForm.brandId)
                this.selectStockChange(this.dataForm.stockId)   // 有stockName,不用写
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      selectSupplyChange($event) {
        this.supplyOptions.forEach(item => {
          if(item.value == $event) {
            this.dataForm.supplyName = item.label
          }
        })
      },
      selectBrandChange($event) {
        this.brandOptions.forEach(item => {
          if(item.value == $event) {
            this.dataForm.brandName = item.label
          }
        })
      },
      selectStockChange($event) {
        this.stockOptions.forEach(item => {
          if(item.value == $event) {
            this.dataForm.stockName = item.label
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              // 更新修改供应商详细信息
              warningSettingEdit(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              addSupplyObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            }
          }else{
            this.ischeck = false
          }
        })
      },

      /*
      * *  获取所需数据字典
      * *     getSupplyDataList  ------- 获取供应商列表数据字典
      * *     getBrandDataList ---- 获取商品品种列表数据字典
      */

     getSupplyList() {
       getSupplyDataList().then(response => {
         if(response.data.code == '0'){
           let dataList = response.data.data.records
           let options = []
           dataList.forEach(item => {
             if(item.status) options.push({value: item.id,label:item.name})
           })
           this.supplyOptions = options
         }else{
           this.$message.error(response.data.msg)
         }
       })
     },
     getBrandDataList() {
       getGoodsBrandDataList().then(response => {
         if(response.data.code == '0'){
           let dataList = response.data.data.records
           let options = []
           dataList.forEach(item => {
             options.push({value:item.id,label:item.name})
           })
           this.brandOptions = options
         }else{
           this.$message.error(response.data.msg)
         }
       })
     },
     getStockList() {
       getStockDataList().then(response => {
         if(response.data.code == '0'){
           let dataList = response.data.data.records
           let options = []
           dataList.forEach(item => {
             options.push({value:item.id,label:item.name})
           })
           this.stockOptions = options
         }else{
           this.$message.error(response.data.msg)
         }
       })
     }
    }
  }
</script>
